package com.song.ejf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.song.ejf.dao.BaseDAO;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SResourceGroup;
import com.song.ejf.pojo.SResourceGroupMap;
import com.song.ejf.service.ResourceGroupService;
import com.song.ejf.util.UUIDUtil;
@Service("resourceGroupService")
public class ResourceGroupServiceImpl implements ResourceGroupService {
	@Autowired
	BaseDAO baseDAO;
	Log log = LogFactory.getLog(ResourceGroupServiceImpl.class);
	
	@Override
	public void loadResourceGroupPage(Page page, String rgName) throws Exception{
		Map<String, String> paraMap = new HashMap<String, String>();
		String selectSql = "select rg_id,resource_group_name,lastmodifytime ";
		String mainSql = " select srg.rg_id,srg.resource_group_name,srg.lastmodifytime" +
				" ,rownum RN from s_resource_group srg where 1=1 ";
		if(StringUtils.isNotBlank(rgName)){
			paraMap.put("resGropName", "%" + rgName.trim() + "%");
			mainSql += " and srg.resource_group_name like :resGropName";
		}
		String order = " order by srg.resource_group_name ";
//		String pageSql =  " where RN > " + page.getStartIndex() + " and RN < " + page.getEndIndex() + " ";
		baseDAO.sqlPageQuery(page, selectSql, mainSql, order, paraMap);
	}

	@Override
	public boolean delResourceGroup(String[] checkItems) throws Exception{
		if(checkItems.length <= 0){
			return false;
		}
		for (int i = 0; i < checkItems.length; i++) {
			baseDAO.delById(SResourceGroup.class, checkItems[i]);
		}
		return true;
	}

	@Override
	public void saveOrUpdateResourceGroup(SResourceGroup resource) throws Exception{
		baseDAO.saveOrUpdate(resource);
	}

	@Override
	public SResourceGroup findResourceGroupById(String id) throws Exception {
		return (SResourceGroup)baseDAO.loadById(SResourceGroup.class, id);
	}

	@Override
	public List<Map<String, String>> loadResGroupMapByRgID(String rgId) throws Exception{
		if(StringUtils.isBlank(rgId)){
			return null;
		}
		Map<String, String> paraMap = new HashMap<String, String>();
		String mainSql = "select res.r_id,srg.rg_id,res.resource_name," +
				"res.resource_url From s_resource res, s_resource_group srg, " +
				"s_resource_group_map srgm where res.r_id = srgm.res_id " +
				" and srg.rg_id = srgm.resgroup_id ";
		mainSql += " and srg.rg_id = :rgId ";
		paraMap.put("rgId", rgId);
		return baseDAO.sqlQuery(mainSql, paraMap);
	}

	@Override
	public List<Map<String, String>> loadAllNoGroupRes()  throws Exception{
		String mainSql = "select r_id,resource_name,resource_url " +
				"From (select res.r_id, res.resource_name, res.resource_url" +
				",rgm.id from s_resource res, s_resource_group_map rgm where " +
				"res.r_id = rgm.res_id(+)) tem where tem.id is null";
		return baseDAO.sqlQuery(mainSql, null);
	}

	@Override
	public boolean updateConfigResGroup(String rgId,String checkItems) throws Exception{
		if(StringUtils.isBlank(rgId)){
			log.error("重新指派资源时,资源组id为空!");
			return false;
		}
		String [] arr = checkItems.split(",");
		//删除原来指派的资源.
		String delSql = "delete s_resource_group_map r where r.resgroup_id = :rgId ";
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("rgId", rgId);
		baseDAO.exeQuery(delSql, paraMap);
		//指派新的资源。
		if(StringUtils.isNotBlank(checkItems) || arr.length >= 0){
			SResourceGroupMap resGroupMap = null; 
			for (int i = 0; i < arr.length; i++) {
				if(StringUtils.isNotBlank(arr[i])){
					resGroupMap = new SResourceGroupMap();
					resGroupMap.setId(UUIDUtil.makeUUID());
					resGroupMap.setResGroupId(rgId);
					resGroupMap.setResId(arr[i].trim());
					baseDAO.saveOrUpdate(resGroupMap);
					resGroupMap = null;
				}
			}
		}
		return true;
	}

	@Override
	public List<Map<String, String>> loadAllGroupedRes()  throws Exception{
		String mainSql = "select res.r_id,res.resource_name,rg.rg_id " +
				"from s_resource res, s_resource_group rg, s_resource_group_map rgm " +
				"where res.r_id = rgm.res_id and rg.rg_id = rgm.resgroup_id";
		return baseDAO.sqlQuery(mainSql, null);
	}

	@Override
	public List<Map<String, String>> loadAllResGroup()  throws Exception{
		String mainSql = "select rg.rg_id,rg.resource_group_name from s_resource_group rg where 1=1";
		return baseDAO.sqlQuery(mainSql, null);
	}

}
